﻿using Microsoft.AspNetCore.Mvc;
using Zhp.Api.Attributes;
using Zhp.Auth;
using Zhp.IService.Sys;
using Zhp.Model.Common;
using Zhp.Model.Sys;

namespace Zhp.Api.Controllers.Sys
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiGroup(GroupName = ApiGroupEnum.Sys)]
    [SeedMenu(MenuGroup = ApiGroupEnum.Sys, Name = "菜单管理", Order = 3, RouterName = "sysmenu", WebFilePath = "sys/sysmenu/index", Icon = "menu")]
    public class SysMenuController : ControllerBase
    {
        protected readonly ISysMenuService SysMenu;

        public SysMenuController(ISysMenuService SysMenu)
        {
            this.SysMenu = SysMenu;
        }

        [HttpPost]
        [SeedList("菜单列表", "菜单管理", ApiGroupEnum.Sys, new string[] { "/api/sysMenu/getSubMenu" })]
        public async Task<PageRet<MenuModel>> Index(MenuQuery query)
        {
            var list = await SysMenu.GetMenu(query);
            
            return new PageRet<MenuModel>
            {
                DataList = list,
                TotalCount = query.TotalCount,
            };
        }

        [HttpGet("{Id}")]
        public async Task<List<MenuModel>> GetSubMenu(long Id)
        {
            return await SysMenu.GetSubMenu(Id);
        }

        [HttpGet()]
        [HttpGet("{Id}")]
        public async Task<ListRet<MenuTreeModel>> GetParentMenuTree(long? Id)
        {
            return new ListRet<MenuTreeModel>
            {
                DataList = await SysMenu.GetParentMenuTree(Id)
            };
        }

        [HttpPost]
        [SeedButton("新增", "菜单列表", "菜单管理", ApiGroupEnum.Sys, new string[] { "/api/sysMenu/getParentMenuTree" })]
        public async Task<bool> Create(MenuCreate Menu)
        {
            return await SysMenu.Create(Menu);
        }

        [HttpPut]
        [SeedButton("修改", "菜单列表", "菜单管理", ApiGroupEnum.Sys)]
        public async Task<bool> Update(MenuCreate Menu)
        {
            return await SysMenu.Update(Menu);
        }

        [HttpDelete("{id}")]
        [SeedButton("删除", "菜单列表", "菜单管理", ApiGroupEnum.Sys)]
        public async Task<bool> Delete(long id)
        {
            return await SysMenu.Delete(id);
        }

        [AllowOnline]
        [HttpGet]
        public async Task<ListRet<HomeMenuTreeModel>> GetHomeMenuList()
        {
            return new ListRet<HomeMenuTreeModel>
            {
               DataList = await SysMenu.GetHomeMenuList() 
            };
        }

        [AllowOnline]
        [HttpGet("{menuId}")]
        public async Task<ListRet<ButtonModel>> GetButtonList(long menuId)
        {
            return new ListRet<ButtonModel>
            {
                DataList = await SysMenu.GetButtonList(menuId)
            };
        }
    }
}
